<?php


namespace app\common\validate;

use app\common\exception\ParameterFailedException;
use think\Validate;

class CommonValidate extends Validate
{
    protected $rule = [
        'page'  => 'integer|between:1,100000000',
        'limit' => 'integer|between:10,100',
    ];

    protected $message = [
        'page.integer'  => 'page参数有误',
        'page.between'  => 'page参数有误',
        'limit.integer' => 'limit参数有误',
    ];
    protected $scene = [
        'paginate' => ['page', 'limit'],
    ];

    /**
     * 验证
     * @param  bool  $scene
     * @param  array  $param
     * @param  bool  $batch
     * @return bool
     * @throws ParameterFailedException
     */
    public function goCheck($scene = false, array $param = [], $batch = false): bool
    {
        $r_params = empty($param) ? \request()->param() : $param;
        if ($scene) {
            $this->scene($scene);
        }
        if ($batch) {
            $this->batch();
        }
        if (!$this->check($r_params)) {
            //            halt($this->error);
            throw new ParameterFailedException(['msg' => $this->error]);
        }
        return true;
    }

    /**
     * 检测是否包含中文
     * @param $value
     * @return bool
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/7/7
     * @Time: 10:46
     */
    protected function notContainsChinese($value): bool
    {
        if (preg_match("/([\x81-\xfe][\x40-\xfe])/", $value)) {
            return false;
        }
        return true;
    }
}